home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / librw / RWTValHashDictionary.z / RWTValHashDictionary
Encoding:
Text File  |  1998-10-30  |  14.5 KB  |  331 lines

  1.  
  2.  
  3.  
  4. RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  5.  
  6.  
  7.  
  8. NNNNaaaammmmeeee
  9.      RWTValHashDictionary<K,V> - Rogue Wave library class
  10.  
  11. SSSSyyyynnnnooooppppssssiiiissss
  12.               #include <rw/tvhdict.h>
  13.  
  14.  
  15.  
  16.               unsigned hashFun(const K&);
  17.           RWTValHashDictionary<K,V> dictionary(hashFun);
  18.  
  19. PPPPlllleeeeaaaasssseeee NNNNooootttteeee!!!!
  20.      IIIIffff yyyyoooouuuu ddddoooo nnnnooootttt hhhhaaaavvvveeee tttthhhheeee SSSSttttaaaannnnddddaaaarrrrdddd CCCC++++++++ LLLLiiiibbbbrrrraaaarrrryyyy,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ddddeeeessssccccrrrriiiibbbbeeeedddd
  21.      hhhheeeerrrreeee....  OOOOtttthhhheeeerrrrwwwwiiiisssseeee,,,, uuuusssseeee tttthhhheeee iiiinnnntttteeeerrrrffffaaaacccceeee ttttoooo RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhMMMMaaaapppp described in the
  22.      Class Reference.
  23.  
  24.  
  25. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  26.      RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<<<<KKKK,,,,VVVV>>>> is a dictionary of keys of type KKKK and values of
  27.      type VVVV, implemented using a hash table.  While duplicates of values are
  28.      allowed, duplicates of keys are not.  It is a vvvvaaaalllluuuueeee based collection:
  29.      keys and values are copied in and out of the hash buckets. Parameters KKKK
  30.      and VVVV represent the type of the key and the type of the value,
  31.      respectively, to be inserted into the table.  These can be either classes
  32.      or fundamental types.  Classes KKKK and VVVV must have:
  33.           well-defined copy semantics (TTTT::::::::TTTT((((ccccoooonnnnsssstttt TTTT&&&&)))) or equivalent);
  34.  
  35.           well-defined assignment semantics (TTTT::::::::ooooppppeeeerrrraaaattttoooorrrr====((((ccccoooonnnnsssstttt TTTT&&&&)))) or
  36.           equivalent).
  37.  
  38.      In addition, class KKKK must have
  39.           well-defined equality semantics (KKKK::::::::ooooppppeeeerrrraaaattttoooorrrr========((((ccccoooonnnnsssstttt KKKK&&&&)))))..in -5
  40.  
  41.           A user-supplied hashing function for type KKKK must be supplied to the
  42.           constructor when creating a new table.  If KKKK is a Rogue Wave class,
  43.           then this requirement is usually trivial because most Rogue Wave
  44.           objects know how to return a hashing value.  In fact, classes
  45.           RRRRWWWWCCCCSSSSttttrrrriiiinnnngggg, RRRRWWWWDDDDaaaatttteeee, RRRRWWWWTTTTiiiimmmmeeee, and RRRRWWWWWWWWSSSSttttrrrriiiinnnngggg contain static member
  46.           functions called hhhhaaaasssshhhh that can be supplied to the constructor as is.
  47.           The function must have prototype:
  48.  
  49.               unsigned hhhhFFFFuuuunnnn(const K& a);
  50.  
  51.  
  52.  
  53.  
  54.  
  55.      and should return a suitable hash value for the object aaaa. To find a
  56.      value, the key is first hashed to determine in which bucket the key and
  57.      value can be found.  The bucket is then searched for an object that is
  58.      equal (as determined by the equality operator) to the key.  The initial
  59.      number of buckets in the table is set by the constructor.  There is a
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  71.  
  72.  
  73.  
  74.      default value.  If the number of (key/value) pairs in the collection
  75.      greatly exceeds the number of buckets then efficiency will sag because
  76.      each bucket must be searched linearly.  The number of buckets can be
  77.      changed by calling member function rrrreeeessssiiiizzzzeeee(((()))).  This is an expensive
  78.      proposition because not only must all the items be copied into the new
  79.      buckets, but all of the keys must be rehashed. If you wish this to be
  80.      done automatically, then you can subclass from this class and implement
  81.      your own special iiiinnnnsssseeeerrrrtttt(((()))) and rrrreeeemmmmoooovvvveeee(((()))) functions which perform a rrrreeeessssiiiizzzzeeee(((())))
  82.      as necessary.
  83.  
  84. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee
  85.      None
  86.  
  87. EEEExxxxaaaammmmpppplllleeee
  88.               #include <rw/tvhdict.h>
  89.           #include <rw/cstring.h>
  90.           #include <rw/rwdate.h>
  91.           #include <rw/rstream.h>
  92.           main()  {
  93.             RWTValHashDictionary<RWCString, RWDate>     birthdays(RWCString::hash);
  94.             birthdays.insertKeyAndValue(
  95.               "John",
  96.               RWDate(12, "April", 1975)
  97.             );
  98.             birthdays.insertKeyAndValue("Ivan", RWDate(2, "Nov", 1980));
  99.             // Alternative syntax:
  100.             birthdays["Susan"] = RWDate(30, "June", 1955);
  101.             birthdays["Gene"] = RWDate(5, "Jan", 1981);
  102.             // Print a birthday:
  103.             cout << birthdays["John"] << endl;
  104.             return 0;
  105.           }
  106.  
  107.  
  108.      Program output:
  109.  
  110.               April 12, 1975
  111.  
  112.  
  113.  
  114. PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  115.  
  116.  
  117.  
  118.  
  119.               RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(unsigned (*hashKey)(const K&),
  120.                                  size_t buckets = RWDEFAULT_CAPACITY);
  121.  
  122.  
  123.      Constructs a new hash dictionary.  The first argument is a pointer to a
  124.      user-defined hashing function for items of type KKKK (the key).  The table
  125.      will initally have bbbbuuuucccckkkkeeeettttssss buckets although this can be changed with
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  137.  
  138.  
  139.  
  140.      member function rrrreeeessssiiiizzzzeeee(((()))).
  141.  
  142.               RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy<K,V>(const RWTValHashDictionary<K,V>&
  143.                                     dict);
  144.  
  145.  
  146.      Copy constructor.  Constructs a new hash dictionary as a copy of ddddiiiicccctttt.
  147.      The new dictionary will have the same number of buckets as the old table.
  148.      Hence, although the keys and values must be copied into the new table,
  149.      the keys will not be rehashed.
  150.  
  151. PPPPuuuubbbblllliiiicccc OOOOppppeeeerrrraaaattttoooorrrrssss
  152.               RWTValHashDictionary<K,V>&
  153.           ooooppppeeeerrrraaaattttoooorrrr====(const RWTValHashDictionary<K,V>& dict);
  154.  
  155.  
  156.      Sets self to a copy of ddddiiiicccctttt.  Afterwards, the new table will have the
  157.      same number of buckets as the old table.  Hence, although the keys and
  158.      values must be copied into the new table, the keys will not be rehashed.
  159.  
  160.               V&
  161.           ooooppppeeeerrrraaaattttoooorrrr[[[[]]]](const K& key);
  162.  
  163.  
  164.      Look up the key kkkkeeeeyyyy and return its associated value as an llllvvvvaaaalllluuuueeee
  165.      reference.  If the key is not in the dictionary, then it is added to the
  166.      dictionary.  In this case, the value associated with the key will be
  167.      provided by the default constructor for objects of type VVVV.
  168.  
  169. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss
  170.               void
  171.           aaaappppppppllllyyyyTTTTooooKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(void (*applyFun)(const K&, V&,void*),
  172.                              void* d);
  173.  
  174.  
  175.      Applies the user-defined function pointed to by aaaappppppppllllyyyyFFFFuuuunnnn to every key-
  176.      value pair in the dictionary.  This function must have prototype:
  177.  
  178.               void yyyyoooouuuurrrrFFFFuuuunnnn(const K& key, V& value, void* d);
  179.  
  180.  
  181.  
  182.  
  183.  
  184.      The key will be passed by constant reference and hence cannot be changed.
  185.      The value will be passed by reference and can be modified.  Client data
  186.      may be passed through as parameter dddd.
  187.  
  188.               void
  189.           cccclllleeeeaaaarrrr();
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  203.  
  204.  
  205.  
  206.      Removes all items from the collection.
  207.  
  208.               RWBoolean
  209.           ccccoooonnnnttttaaaaiiiinnnnssss(const K& key) const;
  210.  
  211.  
  212.      Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to kkkkeeeeyyyy.
  213.      Returns FFFFAAAALLLLSSSSEEEE otherwise.  Equality is measured by the class-defined
  214.      equality operator for class KKKK.
  215.  
  216.               size_t
  217.           eeeennnnttttrrrriiiieeeessss() const;
  218.  
  219.  
  220.      Returns the number of key-value pairs currently in the dictionary.
  221.  
  222.               RWBoolean
  223.           ffffiiiinnnndddd(const K& target, K& retKey) const;
  224.  
  225.  
  226.      Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to ttttaaaarrrrggggeeeetttt
  227.      and puts the matching kkkkeeeeyyyy into rrrreeeettttKKKKeeeeyyyy.  Returns FFFFAAAALLLLSSSSEEEE otherwise and
  228.      leaves rrrreeeettttKKKKeeeeyyyy untouched.  Equality is measured by the class-defined
  229.      equality operator for class KKKK.
  230.  
  231.               RWBoolean
  232.           ffffiiiinnnnddddVVVVaaaalllluuuueeee(const K& key, V& retVal) const;
  233.  
  234.  
  235.      Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to kkkkeeeeyyyy and
  236.      puts the associated vvvvaaaalllluuuueeee into rrrreeeettttVVVVaaaallll.  Returns FFFFAAAALLLLSSSSEEEE otherwise and
  237.      leaves rrrreeeettttVVVVaaaallll untouched.  Equality is measured by the class-defined
  238.      equality operator for class KKKK.
  239.  
  240.               RWBoolean
  241.           ffffiiiinnnnddddKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(const K& key, K& retKey,V& retVal) const;
  242.  
  243.  
  244.      Returns TTTTRRRRUUUUEEEE if the dictionary contains a key which is equal to kkkkeeeeyyyy and
  245.      puts the matching kkkkeeeeyyyy into rrrreeeettttKKKKeeeeyyyy and the associated vvvvaaaalllluuuueeee into rrrreeeettttVVVVaaaallll.
  246.      Returns FFFFAAAALLLLSSSSEEEE otherwise and leaves rrrreeeettttKKKKeeeeyyyy and rrrreeeettttVVVVaaaallll untouched.  Equality
  247.      is measured by the class-defined equality operator for class KKKK.
  248.  
  249.               void
  250.           iiiinnnnsssseeeerrrrttttKKKKeeeeyyyyAAAAnnnnddddVVVVaaaalllluuuueeee(const K& key, const V& value);
  251.  
  252.  
  253.      Inserts the key kkkkeeeeyyyy and value vvvvaaaalllluuuueeee into the dictionary.
  254.  
  255.               RWBoolean
  256.           iiiissssEEEEmmmmppppttttyyyy() const;
  257.  
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))                          RRRRWWWWTTTTVVVVaaaallllHHHHaaaasssshhhhDDDDiiiiccccttttiiiioooonnnnaaaarrrryyyy((((3333CCCC++++++++))))
  269.  
  270.  
  271.  
  272.      Returns TTTTRRRRUUUUEEEE if the dictionary has no items in it, FFFFAAAALLLLSSSSEEEE otherwise.
  273.  
  274.               RWBoolean
  275.           rrrreeeemmmmoooovvvveeee(const K& key);
  276.  
  277.  
  278.      Returns TTTTRRRRUUUUEEEE and removes the (key/value) pair where the key is equal to
  279.      the kkkkeeeeyyyy.  Returns FFFFAAAALLLLSSSSEEEE if there is no such key.  Equality is measured by
  280.      the class-defined equality operator for class KKKK.
  281.  
  282.               void
  283.           rrrreeeessssiiiizzzzeeee(size_t N);
  284.  
  285.  
  286.      Changes the number of buckets to NNNN, a relatively expensive operation if
  287.      there are many items in the collection.
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.